package com.q.course.binder.common;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.hibernate.internal.util.StringHelper;
import org.springframework.core.convert.converter.Converter;

public class CustomerConverter implements Converter<String, Date> {  
	
	@Override  
	public Date convert(String source) {  
		
		if(StringHelper.isNotEmpty(source)){
			  
		    
		    if(source.indexOf("年")>0){
		    	SimpleDateFormat dateFormat2 = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分");
		    	dateFormat2.setLenient(false);
		    	try {  
			        return dateFormat2.parse(source);  
			    } catch (ParseException e) {  
			        
			    	throw new IllegalArgumentException("Could not parse date: " + e.getMessage(), e);
			    } 
		    }
		    else if (source.indexOf(":")>0) {
		    	SimpleDateFormat dateFormat3 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		    	dateFormat3.setLenient(false);
		    	try {  
			        return dateFormat3.parse(source);  
			    } catch (ParseException e) {  
			        
			    	throw new IllegalArgumentException("Could not parse date: " + e.getMessage(), e);
			    } 
			}
		    else{
		    	SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");  
			    dateFormat.setLenient(false);
		    	try {  
			        return dateFormat.parse(source);  
			    } catch (ParseException e) {  
			        
			    	throw new IllegalArgumentException("Could not parse date: " + e.getMessage(), e);
			    } 
		    }
		}
		return null;
	}
}
